package com.ns.core.utils.sms;


import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpHeaders;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.ns.core.utils.sg.ResultError;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class AliSmsUtils {

    private static final String ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI5t8cBDQKEvYs2CeDz8mD";
    private static final String ALIBABA_CLOUD_ACCESS_KEY_SECRET = "iWElfJS6woj26htSBPhxnMSoX5SyO9";
    private static final String DY_SMS_API = "dysmsapi.aliyuncs.com";
    private static final String REGION = "cn-chengdu";

    public static AsyncClient getClient(String appKey, String appSecret, String smsApi, String region) {
        // Configure Credentials authentication information, including ak, secret, token
        if (!StringUtils.hasText(appKey)) {
            appKey = ALIBABA_CLOUD_ACCESS_KEY_ID;
        }
        if (!StringUtils.hasText(appSecret)) {
            appSecret = ALIBABA_CLOUD_ACCESS_KEY_SECRET;
        }
        if (!StringUtils.hasText(smsApi)) {
            smsApi = DY_SMS_API;
        }
        if (!StringUtils.hasText(region)) {
            region = REGION;
        }
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(appKey)
                .accessKeySecret(appSecret)
                .build());
        // Configure the Client
        return AsyncClient.builder()
                .region(region)
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(smsApi)
                                .setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
    }

    /**
     * 发送阿里短信
     *
     * @param sign         签名
     * @param mobile       电话
     * @param templateCode 模板code
     * @param param        参数
     * @return
     * @throws Exception 异常
     */
    public static SendSmsResponse sendSms(String appKey, String appSecret, String smsApi, String region, String sign, String mobile, String templateCode, String param) {
        AsyncClient client = getClient(appKey, appSecret, smsApi, region);
        try {
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName(sign)
                    .phoneNumbers(mobile)
                    .templateCode(templateCode)
                    .templateParam(param)
                    .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                    .build();
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            return response.get();
        } catch (InterruptedException e) {
            throw new ResultError(e.getMessage());
        } catch (ExecutionException e) {
            throw new ResultError(e.getMessage());
        } finally {
            client.close();
        }
    }

    public static void main(String[] args) {
        sendSms(null, null, null, "cn-chengdu", "JUYO·桔友", "18328435094", "SMS_182400134", "{'code':'1345'}");
    }
}
